from selenium import webdriver
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time

# 设置浏览器、启动浏览器
def create_driver():
    options = Options()
    options.add_argument('--no-sandbox')
    options.add_experimental_option("detach", True)
    driver_edge = webdriver.Edge(options=options)
    return driver_edge
driver = create_driver()

driver.get('https://mail.qq.com/')
driver.maximize_window()
driver.implicitly_wait(30)
iframe_list = driver.find_elements(By.TAG_NAME, 'iframe')
print(len(iframe_list))

# 获取iframe元素
iframe_el_one = driver.find_element(By.XPATH, '//*[@id="QQMailSdkTool_login_loginBox_qq"]/iframe')
# 进入到iframe嵌套界面
driver.switch_to.frame(iframe_el_one)
print('已经进入到第一个iframe界面了')

iframe_el_two = driver.find_element(By.XPATH, '//*[@id="ptlogin_iframe"]')
driver.switch_to.frame(iframe_el_two)
print('已经进入到第二个iframe界面了')

loginButton = driver.find_element(By.XPATH, '/html/body/div[1]/div[10]/a[1]')
if loginButton.text == "密码登录":
    loginButton.click()
    print('点击了密码登录')
driver.implicitly_wait(30)

driver.find_element(By.XPATH, '//*[@id="u"]').send_keys('1552474482@qq.com')
driver.find_element(By.XPATH, '//*[@id="p"]').send_keys('123456')
driver.implicitly_wait(30)
driver.find_element(By.XPATH, '//*[@id="login_button"]').click()
print("点击了登录按钮")

driver.switch_to.default_content()
driver.implicitly_wait(30)
driver.switch_to.default_content()
driver.implicitly_wait(30)

qq_test = driver.find_element(By.XPATH, '//*[@id="login_pictures"]/div[2]/p[1]')
if qq_test.text == 'QQ邮箱，常联系！':
    print('回到了最开始的界面')
else:
    print("你还在iframe界面")

driver.close()
